$(function(){
    $("#searchPersonAgeCount_dategrid").datagrid_init;
    /*==初始化表格*/
    $('#searchPersonAgeCount_dategrid').datagrid({
        loadMsg : "数据加载中",
        emptyMsg : "数据加载完成,当前无数据!",
        border:false,
        fit : true,
        title : "人员年龄分布表",
        fitColumns : true,
        pagination : false,
        rownumbers : true,
        singleSelect : true,
        // toolbar:"#searchPersonAgeCount_dategrid_toolbar",
        columns : [ [
            {field : 'id',				title : '主键',			width:50 , align:'center',hidden:true},
            {field : 'age',				title : '年龄',			width:50 , align:'center',hidden:false,
                formatter: function(value,row,index){
                    return 	value;
                }
            },
            {field : 'count',			title : '人数 ',		width:50 , align:'center',hidden:false,
                formatter: function(value,row,index){
                    return 	value;
                }
            }
        ] ],
        onLoadSuccess:function(data){
            $(this).datagrid('doCellTip',{'max-width':'300px','delay':500});
        }
    });

    var params = {};
    $("#searchPersonAgeCount_dategrid").datagrid_pagination(basepath+"/user/v1/selectPersonAgeCount",JSON.stringify(params),"","" );

    /*查询按钮*/
    /*$("#searchPersonAgeCount_btn_search").linkbutton({
        onClick:function(){
            var name = $("#searchPersonAgeCount_Name").textbox("getValue");
            var datebegin=$.trim($("#searchPersonAgeCount_DateBegin").datetimebox('getValue'));
            var dateend=$.trim($("#searchPersonAgeCount_DateEnd").datetimebox('getValue'));
            var params = {};
            params.name = name;
            params.datebegin = datebegin;
            params.dateend = dateend;
            $("#searchPersonAgeCount_dategrid").datagrid_pagination(basepath+"/user/v1/selectPersonAgeCount",JSON.stringify(params),"","" );
        }
    });*/

    loadHichart();

});

/**
 * 加载柱状图
 */
function loadHichart(){
   var params = {};
    $.ajax({
        type : "post",
        url : basepath + '/user/v1/selectPersonAgeCount',
        dataType : "json",
        contentType : "application/json;charset=utf-8;",
        data : JSON.stringify(params),
        success : function(data) {
            if(data.result == "0"){
                $.messager.alert("提示","系统异常,请联系管理员!"+data.msg,"error");
                return;
            }
            console.dir(data);
            var rows = data.rows;
            if(rows == null || rows.length == 0){
                return;
            }
            var normalAll = [];
            for (var i = 0; i < rows.length; i++) {
                var normal = [];
                normal[0] = rows[i].age;
                normal[1] = rows[i].count;
                normalAll[i]=normal;
            }

            //加载highcharts
            $('#main_container').highcharts({
                chart: {
                    plotBackgroundColor: null,
                    plotBorderWidth: null,
                    plotShadow: false
                },
                title: {
                    text: '人员年龄分布图'
                },
                credits: {
                    enabled: false
                    // text: 'Example.com',
                    // href: 'http://www.example.com'
                },
                tooltip: {
                    headerFormat: '{series.name}<br>',
                    pointFormat: '{point.name}: <b>{point.percentage:.1f}%</b>'
                },
                plotOptions: {
                    pie: {
                        allowPointSelect: true,
                        cursor: 'pointer',
                        dataLabels: {
                            enabled: true,
                            format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                            style: {
                                color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                            }
                        }
                    }
                },
                series: [{
                    type: 'pie',
                    name: '人员年龄分布图',
                    data : normalAll
                }]
            });

        }
    });


}
	